﻿using System;
using System.Collections.Generic;
using System.Text;
using TimeTable;
using System.Data;
using Database;

namespace DB
{
    public class LessonsMapper:IMapper
    {
        List<Entity> lessonsList;
        Lesson lesson;
        DataTable dt;

        void OpenConnection()
        {
            AccessDb.Instance.FileName = @"C:\Documents and Settings\Ирина\Рабочий стол\Project\DB\db1.mdb";
            AccessDb.Instance.OpenConnection();
            dt = AccessDb.Instance.GetTable("Lessons");
        }
        void CloseConnection()
        {
            AccessDb.Instance.Commit();
            AccessDb.Instance.CloseConnection();
        }

        #region IMapper Members

        public List<Entity> LoadAll()
        {
            lessonsList = new List<Entity>();
            AccessDb.Instance.OpenConnection();
            foreach (DataRow dr in dt.Rows)
            {
                lesson = new Lesson();
                lesson.Group = dr[0].ToString();
                lesson.Subject.Subject = dr[1].ToString();
                lesson.Date = Convert.ToDateTime(dr[2]);
                lesson.Theme = dr[3].ToString();
                if (dr[4].ToString() == LessonTypes.Дипломное.ToString())
                    lesson.LessonType = LessonTypes.Дипломное;
                if (dr[4].ToString() == LessonTypes.Курсовое.ToString())
                    lesson.LessonType = LessonTypes.Курсовое;
                if (dr[4].ToString() == LessonTypes.Лекции.ToString())
                    lesson.LessonType = LessonTypes.Лекции;
                if (dr[4].ToString() == LessonTypes.Практика.ToString())
                    lesson.LessonType = LessonTypes.Практика;
                lesson.Hours = Convert.ToInt32(dr[5]);
                lessonsList.Add(lesson);
            }
            AccessDb.Instance.CloseConnection();
            return lessonsList;
        }

        public Entity Find(Entity e)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void Insert(Entity e)
        {
            lesson = e as Lesson;
            OpenConnection();
            DataRow dr = dt.NewRow();            
            dr[0] = lesson.Group;
            dr[1] = lesson.Subject;
            dr[2] = lesson.Date;
            dr[3] = lesson.Theme;
            dr[4] = lesson.LessonType;
            dr[5] = lesson.Hours;
            dt.Rows.Add(dr);
            CloseConnection();
        }

        public void DeleteEntity(Entity e)
        {
            lesson = e as Lesson;
            OpenConnection();
            foreach (DataRow dr in dt.Rows)
            {
                if ((lesson.Group == dr[0].ToString()) && (lesson.Subject.Subject == dr[1].ToString()) && (lesson.Date == Convert.ToDateTime(dr[2])) && (lesson.Theme == dr[3].ToString()) && (lesson.LessonType.ToString() == dr[4].ToString()))
                {
                    dr.Delete();
                    CloseConnection();
                    return;
                }
            }
            AccessDb.Instance.CloseConnection();
        }

        public void UpdateEntity(Entity e)
        {
            lesson = e as Lesson;
            OpenConnection();
            foreach (DataRow dr in dt.Rows)
            {
                if ((lesson.Group == dr[0].ToString()) && (lesson.Subject.Subject == dr[1].ToString()) && (lesson.Date == Convert.ToDateTime(dr[2])) && (lesson.Theme == dr[3].ToString()) && (lesson.LessonType.ToString() == dr[4].ToString()))
                {
                    dr[0] = lesson.Group;
                    dr[1] = lesson.Subject;
                    dr[2] = lesson.Date;
                    dr[3] = lesson.Theme;
                    dr[4] = lesson.LessonType;
                    dr[5] = lesson.Hours;                                      
                }
            }
            CloseConnection();
        }

        #endregion

        #region IMapper Members


        public void ClearAll()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }
}
